package com.ling.pl.security.model;

import com.ling.pl.core.commons.annotation.ModelProperty;
import com.ling.pl.core.commons.constant.TablePreDef;
import com.ling.pl.core.hibernate.identifier.Ling2UUIDGenerator;
import com.ling.pl.core.model.impl.BaseEntity;
import com.ling.pl.log.service.Log;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;

/**
 * @author ling2
 */
@Entity
@Table(name = DefaultOrg.TABLE)
public class DefaultOrg extends BaseEntity {

    public static final String TABLE = TablePreDef.BASEPRE + "ORG";
    public static final String SEQ = TABLE;

    @Id
    @GenericGenerator(name = SEQ + "_GENERATOR", strategy = Ling2UUIDGenerator.STRATEGY_NAME, parameters = {@Parameter(name = "pkColumnValue", value = SEQ)})
    @GeneratedValue(strategy = GenerationType.TABLE, generator = SEQ + "_GENERATOR")
    @Column(name = "ORG_ID", length = 36)
    String id;

    @Column(name = "ORG_CODE", length = 100)
    @ModelProperty(comment = "组织代码")
    String orgCode;

    @Column(name = "ORG_NAME", length = 120)
    @ModelProperty(comment = "组织名称")
    String orgName;

    @Column(name = "ORG_TYPE", length = 120)
    @ModelProperty(comment = "组织类型")
    String orgType;

    @Column(name = "DESCRIPTION", length = 120)
    @ModelProperty(comment = "描述")
    String des;

    @Column(name = "PARENT_ID", length = 36)
    @ModelProperty(comment = "父结点ID")
    String parentId;

    @Column(name = "PARENTNAME", length = 36)
    @ModelProperty(comment = "父结点名称")
    String parentName;

    @Column(name = "IS_VIRTUAL")
    @ModelProperty(comment = "是否虚拟组织,实体单位=true,虚拟单位=false")
    Boolean virtual;

    @Transient
    DefaultOrg parent;
    @Transient
    List<DefaultUser> users;
    @Transient
    List<DefaultOrg> children;

    public DefaultOrg() {

    }

    public DefaultOrg(String orgId) {
        this.id = orgId;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public DefaultOrg getParent() {
        return parent;
    }

    public void setParent(DefaultOrg parent) {
        this.parent = parent;
    }

    public List<DefaultUser> getUsers() {
        return users;
    }

    public void setUsers(List<DefaultUser> users) {
        this.users = users;
    }

    public List<DefaultOrg> getChildren() {
        return children;
    }

    public void setChildren(List<DefaultOrg> children) {
        this.children = children;
    }

    public Boolean getVirtual() {
        return virtual;
    }

    public void setVirtual(Boolean virtual) {
        this.virtual = virtual;
    }

    public String getOrgCode() {
        return orgCode;
    }

    public void setOrgCode(String orgCode) {
        this.orgCode = orgCode;
    }

    public String getOrgName() {
        return orgName;
    }

    public void setOrgName(String orgName) {
        this.orgName = orgName;
    }

    public String getOrgType() {
        return orgType;
    }

    public void setOrgType(String orgType) {
        this.orgType = orgType;
    }

    public String getDes() {
        return des;
    }

    public void setDes(String des) {
        this.des = des;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public String getParentName() {
        return parentName;
    }

    public void setParentName(String parentName) {
        this.parentName = parentName;
    }

    public String toLogString(String operationType, Log log) {
        ///todo
//        IDao dao = SpringUtil.getBean(BaseDao.BEAN_ID);
        String orgInfo = "机构名称:" + getOrgName() + " 机构代码:" + getOrgCode() + " 机构类型:" + getOrgType() + " 描述:" + getDes();
//		if(getParentId()!=null&&dao!=null){
//			DefaultOrg parentOrg=(DefaultOrg) dao.get(DefaultOrg.class,getParentId());
//			if(parentOrg!=null){
//				orgInfo=orgInfo+" 上级机构:"+parentOrg.getOrgName();
//			}
//		}
        return orgInfo;
    }

    public Serializable getBusinessId() {
        return getId();
    }

}
